<template>
  <el-dialog
    v-model="dialogVisible"
    :title="isAdd ? '新增模板项' : '编辑模板项'"
    width="500px"
    @close="resetForm"
  >
    <el-form ref="serviceForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="服务类型" prop="standardCode">
        <el-select
          v-model="form.standardCode"
          placeholder="请选择"
          filterable
          clearable
          class="!w-[260px]"
        >
          <el-option
            v-for="item in taskConfigStore.serviceTypes"
            :key="item.standardCode"
            :label="item.displayName"
            :value="item.standardCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input-number v-model="form.price" class="!w-[260px]" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" :loading @click="handleConfirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed } from "vue";
import { message } from "@/utils/message";
import { useTaskConfigHook } from "@/store/modules/taskConfig";
import {
  createAdminTemplateItem,
  updateAdminTemplateItem
} from "@/api/adminTemplateApi";

const emits = defineEmits(["refresh"]);
const taskConfigStore = useTaskConfigHook();
const dialogVisible = ref(false);
const loading = ref(false);
const form = ref({
  price: 0,
  standardCode: ""
});
const itemId = ref("");
// 基础校验规则
const rules = {
  price: [
    { required: true, message: "请输入价格", trigger: "blur" },
    {
      pattern: /^[-+]?(\d+(\.\d+)?|\.\d+)$/,
      message: "请输入正确的数字",
      trigger: "blur"
    },
    {
      validator: (rule, value, callback) => {
        if (value <= 0) {
          callback(new Error("价格必须大于0"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  standardCode: [
    {
      required: true,
      message: "请选择服务类型",
      trigger: "change"
    }
  ]
};
const isAdd = computed(() => !itemId.value);
const serviceForm = ref(null);
// 重置表单
const resetForm = () => {
  itemId.value = "";
  form.value = {
    price: 0,
    standardCode: ""
  };
  serviceForm.value?.clearValidate();
  serviceForm.value?.resetFields();
};

const handleConfirm = () => {
  serviceForm.value.validate(async valid => {
    if (valid) {
      try {
        loading.value = true;
        if (isAdd.value) {
          const { message: text } = await createAdminTemplateItem(form.value);
          message(text || "创建成功", { type: "success" });
        } else {
          const { message: text } = await updateAdminTemplateItem(
            itemId.value,
            form.value
          );
          message(text || "更新成功", { type: "success" });
        }
        emits("refresh");
        dialogVisible.value = false;
      } catch (error) {
        console.log(error);
      } finally {
        loading.value = false;
      }
    }
  });
};
const open = (row: { id: string; price: number; standardCode: string }) => {
  if (row?.id) {
    itemId.value = row.id;
    form.value = {
      price: row.price,
      standardCode: row.standardCode
    };
  }
  dialogVisible.value = true;
};
defineExpose({
  open
});
</script>
